<?php

$userid = get_loggedin_userid();
$client_id = get_input('client_id');
$json_facebook_ids = get_input('Facebook_ids');
$json_gowalla_ids = get_input('Gowalla_ids');
$json_foursquare_ids = get_input('Foursquare_ids');
$json_contact_emails = get_input('Apple_ids');
$brief = get_input('brief');

if($json_facebook_ids) {
    $facebook_ids = json_decode($json_facebook_ids, true);
}

if ($json_gowalla_ids) {
    $gowalla_ids = json_decode($json_gowalla_ids, true);
}
if ($json_foursquare_ids) {
    $foursquare_ids = json_decode($json_foursquare_ids, true);
}

$userEntities = array();
$userEntitiesFromContact = array();

if($json_contact_emails) {
    $contact_emails = json_decode($json_contact_emails, true);
    foreach ($contact_emails as $contact_email) {
        $userEntity = array_shift(get_user_by_email($contact_email));
        if($userEntity) {
        	$userEntitiesFromContact[$userEntity->guid] = $userEntity;
        }
    }
}

$provider_ids = array();


foreach ($gowalla_ids as $gowalla_id) {
    $provider_ids[] = array(
        'id' => $gowalla_id,
        'type' => PROVIDER_ACCOUNT_TYPE_GOWALLA
    );
}

foreach ($foursquare_ids as $foursquare_id) {
    $provider_ids[] = array(
        'id' => $foursquare_id,
        'type' => PROVIDER_ACCOUNT_TYPE_FOURSQUARE
    );
}

$userEntities = greengar_opinions_suggest_friends($provider_ids);

$facebookEntities = array();
foreach($facebook_ids as $facebook_id) {
    $facebookAccounts = greengar_get_facebook($facebook_id, NULL);
    if($facebookAccounts && $facebookAccounts[0]) {
        $facebookAccount = $facebookAccounts[0];
        $friendid = $facebookAccount->owner_guid;
        $friendEntity = get_user($friendid);
        $facebookEntities[] = $friendEntity;
    }
}

$userEntities = array_merge($userEntities, $facebookEntities);
$userEntities = array_merge($userEntities, $userEntitiesFromContact);

$results = array();
foreach ($userEntities as $userEntity) {
    $results[] = userToArray($userEntity, $client_id, $brief);
}

/*
* temporarily function for user action - we will design new user system.
*/
function greengar_opinions_count_providerAccount($id, $userid, $type) {
    $pairs = array();
    if ($id !== NULL) {
        $pairs[] = array('name' => PROVIDER_ACCOUNT_ID, 'value' => $id, 'operand' => '=', 'case_sensitive' => FALSE);
    }
    if ($type !== NULL) {
        $pairs[] = array('name' => PROVIDER_ACCOUNT_TYPE, 'value' => $type, 'operand' => '=', 'case_sensitive' => FALSE);
    }
    if ($userid == ELGG_ENTITIES_ANY_VALUE) {
        $ownerids = ELGG_ENTITIES_ANY_VALUE;
    } else {
        $ownerids = array($userid);
    }
    $options = array(
        'types' => PROVIDER_ACCOUNT_PARENT,
        'subtypes' => PROVIDER_ACCOUNT_SUBTYPE,
        'metadata_names' => NULL,
        'metadata_values' => NULL,
        'metadata_name_value_pairs' => $pairs,
        'metadata_name_value_pairs_operator' => 'AND',
        'metadata_case_sensitive' => FALSE,
        'order_by_metadata' => array(),
        'metadata_owner_guids' => $ownerids,
        'count' => TRUE
    );
    $count = elgg_get_entities_from_metadata($options);
    return $count;
}

function greengar_opinions_get_providerAccount($id, $userid, $type) {
    $pairs = array();
    if ($id !== NULL) {
        $pairs[] = array('name' => PROVIDER_ACCOUNT_ID, 'value' => $id, 'operand' => '=', 'case_sensitive' => FALSE);
    }
    if ($type !== NULL) {
        $pairs[] = array('name' => PROVIDER_ACCOUNT_TYPE, 'value' => $type, 'operand' => '=', 'case_sensitive' => FALSE);
    }
    if ($userid == ELGG_ENTITIES_ANY_VALUE) {
        $ownerids = ELGG_ENTITIES_ANY_VALUE;
    } else {
        $ownerids = array($userid);
    }
    $options = array(
        'types' => PROVIDER_ACCOUNT_PARENT,
        'subtypes' => PROVIDER_ACCOUNT_SUBTYPE,
        'metadata_names' => NULL,
        'metadata_values' => NULL,
        'metadata_name_value_pairs' => $pairs,
        'metadata_name_value_pairs_operator' => 'AND',
        'metadata_case_sensitive' => FALSE,
        'order_by_metadata' => array(),
        'metadata_owner_guids' => $ownerids,
        'limit' => 10000,
        'count' => FALSE
    );
    $entities = elgg_get_entities_from_metadata($options);
    return $entities;
}

function greengar_opinions_suggest_friends($provider_ids) {

	$results = array();
	foreach($provider_ids as $provider_id) {
            
                $id = $provider_id['id'];
                $type = $provider_id['type'];
                
		$entities = greengar_opinions_get_providerAccount($id, $userid, $type);
                
		if($entities && $entities[0]) {
			$userid = $entities[0]->owner_guid;
			$userEntity = get_entity($userid);
			$results[$userid] = $userEntity;
		} else {

		}
	}
	return $results;
}

/*
* end
*/

echo json_encode(export_result(200, $results));
?>